package com.tj.api.rest.user;

import com.alibaba.fastjson.JSONObject;
import com.fqgj.common.api.annotations.ParamsValidate;
import com.fqgj.log.util.Constants;
import com.tj.api.annotations.VisitorAccessible;
import com.tj.bo.common.conf.api.ApiResponse;
import com.tj.bo.common.conf.config.FileConfigUtil;
import com.tj.bo.common.conf.config.RedisConfigUtil;
import com.tj.bo.user.client.UserRegisterService;
import com.tj.bo.user.client.vo.input.UserChannelRepairInput;
import com.tj.template.application.user.output.AppConfigApOutput;
import com.tj.template.config.vo.RequestBasicInfo;
import com.tj.template.config.vo.RequestLocalInfo;
import com.tj.template.config.vo.UserAccessInfo;
import org.apache.logging.log4j.ThreadContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * User: binary
 * Date: 2020/6/9
 * Time: 下午4:35
 */
@RestController
@RequestMapping("/user")
public class AppController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AppController.class);

    @Autowired
    private RedisConfigUtil redisDbConfigUtil;

    @Autowired
    private UserRegisterService userRegisterService;

    @Autowired
    private FileConfigUtil fileConfigUtil;

    /**
     * 升级接口
     * 0-无需升级 1-建议升级 2-强制升级
     *
     * @return
     */
    @VisitorAccessible
    @RequestMapping(value = "/config/get")
    @ParamsValidate
    public ApiResponse<AppConfigApOutput> config() {
        Integer versionCode = RequestLocalInfo.getRequestBasicInfo().getVersionCode();
        String version = "V" + RequestLocalInfo.getRequestBasicInfo().getAppVersion();
        Integer updateMode = 0;
        if (versionCode < redisDbConfigUtil.getMiniAndroidVersionCode()) {
            updateMode = 1;
            if (redisDbConfigUtil.openForceUpdate()) {
                updateMode = 2;
            }
        }
        LOGGER.info("============== 0-无需升级 1-建议升级 2-强制升级 updateMode:{},versionCode:{},miniUpdateVersionCode:{} ============== ", updateMode, versionCode, redisDbConfigUtil.getMiniAndroidVersionCode());
        AppConfigApOutput appConfigApOutput = new AppConfigApOutput(redisDbConfigUtil.getUpdateDesc(), 1, updateMode, version);

        //渠道号修复
        RequestBasicInfo requestBasicInfo = RequestLocalInfo.getRequestBasicInfo();
        UserAccessInfo userAccessInfo = RequestLocalInfo.getCurrentUser();
        UserChannelRepairInput userChannelRepairInput = new UserChannelRepairInput();
        userChannelRepairInput.setAppCode(requestBasicInfo.getAppCode());
        userChannelRepairInput.setUserCode(userAccessInfo == null ? null : userAccessInfo.getUserCode());
        userChannelRepairInput.setDeviceId(requestBasicInfo.getDeviceId());
        userChannelRepairInput.setChannel(requestBasicInfo.getChannel());
        userChannelRepairInput.setMessage(ThreadContext.get(Constants.TRACE_ID));
        userRegisterService.registerChannelRepairAsync(userChannelRepairInput);

        return new ApiResponse<>(appConfigApOutput);
    }


    /**
     * websocket接口
     *
     * @return
     */
    @VisitorAccessible
    @RequestMapping(value = "/websocket/host/get")
    @ParamsValidate
    public ApiResponse getWebsocketHost() {
        JSONObject o = new JSONObject();
        //ws://149.129.136.180:9000/ws
        //wss://socket.abctradeapp.com/ws
        o.put("hostUrl", redisDbConfigUtil.getWebSocketServerHost());
        return new ApiResponse(o);
    }
}
